//动态生成声明文件
//引入所有文件
const fs = require("fs");
const _ = require("lodash");
async function typings() {
  if (process.env.npm_lifecycle_event == "start") return;
  const fileBase = __dirname + "/../../../";
  const fd = fs.openSync(fileBase + "typings/young.d.ts", "w");
  let importString = `import { Repository } from "typeorm";\n`;
  let orm = "";
  let service = "";
  let comm = "";
  let queue = "";
  let es = "";
  let socket = "";
  const moduleFileExists = await fs.existsSync(fileBase + "module");
  if (moduleFileExists) {
    require("young-require-all")({
      dirname: fileBase + "module",
      filter: /(.+)\.ts$/,
      resolve: function (Service, filePath) {
        for (let ServiceName in Service) {
          let name = "Service" + Service[ServiceName].name;
          let fp = filePath.replace(fileBase, "../").replace(".ts", "");
          importString += `import ${name} from "${fp}";\n`;
          service += `\t\t\t\t${Service[ServiceName].name}: ${name};\n`;
        }
      },
    });
  }
  const commFileExists = await fs.existsSync(fileBase + "comm");
  if (commFileExists) {
    require("young-require-all")({
      dirname: fileBase + "comm",
      filter: /(.+)\.ts$/,
      resolve: function (Service, filePath) {
        for (let ServiceName in Service) {
          let name = "Service" + Service[ServiceName].name;
          let fp = filePath.replace(fileBase, "../").replace(".ts", "");
          importString += `import ${name} from "${fp}";\n`;
          comm += `\t\t\t\t${Service[ServiceName].name}: ${name};\n`;
        }
      },
    });
  }
  const entityFileExists = await fs.existsSync(fileBase + "entity");
  if (entityFileExists) {
    require("young-require-all")({
      dirname: fileBase + "entity",
      filter: /(.+)\.ts$/,
      resolve: function (Service, filePath) {
        for (let ServiceName in Service) {
          let name = "Entity" + Service[ServiceName].name;
          let fp = filePath.replace(fileBase, "../").replace(".ts", "");
          importString += `import ${name} from "${fp}";\n`;
          orm += `\t\t\t\t${Service[ServiceName].name}: Repository<${name}>;\n`;
        }
      },
    });
  }
  const queueFileExists = await fs.existsSync(fileBase + "queue");
  if (queueFileExists) {
    require("young-require-all")({
      dirname: fileBase + "queue",
      filter: /(.+)\.ts$/,
      resolve: function (Service) {
        for (let ServiceName in Service) {
          let name = Service[ServiceName].name;
          queue += `\t\t\t\t${name}: typeof Bull;\n`;
        }
      },
    });
  }
  const esindexFileExists = await fs.existsSync(fileBase + "esindex");
  if (esindexFileExists) {
    require("young-require-all")({
      dirname: fileBase + "esindex",
      filter: /(.+)\.ts$/,
      resolve: function (Service, filePath, fileName) {
        es += `\t\t\t\t${fileName}: any;\n`;
      },
    });
  }
  //socket
  const socketFileExists = await fs.existsSync(fileBase + "socket");
  if (socketFileExists) {
    require("young-require-all")({
      dirname: fileBase + "socket",
      filter: /(.+)\.ts$/,
      resolve: function (Service, filePath) {
        for (let ServiceName in Service) {
          let name = "Service" + Service[ServiceName].name;
          let fp = filePath.replace(fileBase, "../").replace(".ts", "");
          importString += `import ${name} from "${fp}";\n`;
          socket += `\t\t\t\t${Service[ServiceName].name}: ${name};\n`;
        }
      },
    });
  }
  es = _.trim(es, "\n");
  queue = _.trim(queue, "\n");
  orm = _.trim(orm, "\n");
  comm = _.trim(comm, "\n");
  service = _.trim(service, "\n");
  socket = _.trim(socket, "\n");
  importString = _.trim(importString, "\n");
  const appString = `{
      orm: {
${orm}
      };
      config;
      comm: {
${comm}
      };
      service: {
${service}
      };
      redis: any;
      env: String;
      queue: {
${queue}
      };
      task: typeof Bull;
      es: {
${es}
      };
      log: {
        info(msg: String);
        warn(msg: String);
        error(msg: String);
        success(msg: String);
      };
      socket: {
        io: Server;
${socket}
      };
    };`;
  const appString1 = `{
    orm: {
${orm.replace(/\t\t\t\t/g, "\t\t\t")}
    };
    config;
    comm: {
${comm.replace("\t\t\t\t", "\t\t\t")}
    };
    service: {
${service.replace(/\t\t\t\t/g, "\t\t\t")}
    };
    redis: any;
    env: String;
    queue: {
${queue.replace(/\t\t\t\t/g, "\t\t\t")}
    };
    task: typeof Bull;
    es: {
${es.replace(/\t\t\t\t/g, "\t\t\t")}
    };
    log: {
      info(msg: String);
      warn(msg: String);
      error(msg: String);
      success(msg: String);
    };
    socket: {
      io: Server;
${socket.replace(/\t\t\t\t/g, "\t\t\t")}
    };
  }`;
  const string = `${importString}
import Bull from "bull";
import { Server } from "socket.io";
declare module "young-core" {
  interface youngService {
    app: ${appString}
  }
  interface youngTask {
    app: ${appString}
  }
  interface youngSocket {
    app: ${appString}
  }
  interface Application ${appString1}
}
`;
  fs.writeSync(fd, string);
  fs.closeSync(fd);
}
module.exports = { typings };
